package array

import (
	"fmt"
	"math/rand"
	"time"
)

func DeclareList() {
	var arr1 [3]int = [3]int{}
	var arr2 = [3]int{1, 2, 3}
	var arr3 = [...]int{4, 5, 6}
	var arr4 = [...]int{1: 8, 0: 9, 2: 7}
	fmt.Printf("arr1: %v\n", arr1)
	fmt.Printf("arr2: %v\n", arr2)
	fmt.Printf("arr3: %v\n", arr3)
	fmt.Printf("arr4: %v\n", arr4)
}

func CalWeight() {
	hen1 := 3.0
	hen2 := 5.0
	hen3 := 1.0
	hen4 := 3.0
	hen5 := 2.0
	hen6 := 6.0

	totalWeight := hen1 + hen2 + hen3 + hen4 + hen5 + hen6
	avgWeight := fmt.Sprintf("%.2f", totalWeight/6)
	fmt.Printf("totalWeight = %v; avgWeight = %v\n", totalWeight, avgWeight)

	var hens []float64 = []float64{6.0, 10.0, 2.0, 6.0, 4.0, 12.0}
	var total float64
	for _, v := range hens {
		total = total + v
	}
	var avg = fmt.Sprintf("%.2f", total/float64(len(hens)))
	fmt.Printf("total: %v\n", total)
	fmt.Printf("avg: %v\n", avg)
}

func CalScore(scoreList []float64) (float64, float64) {
	var total float64
	for i := 0; i < len(scoreList); i++ {
		fmt.Printf("请输入第%v个学生的成绩: ", i+1)
		fmt.Scanln(&scoreList[i])
		total += scoreList[i]
	}
	avg := total / float64(len(scoreList))
	return total, avg
}

func ReverseList() {
	var arr [5]int
	rand.Seed(time.Now().Unix())
	length := len(arr)
	for i := 0; i < length; i++ {
		arr[i] = rand.Intn(100)
	}
	fmt.Printf("arr: %v\n", arr)
	var temp int
	for i := 0; i < length/2; i++ {
		temp = arr[i]
		arr[i] = arr[length-1-i]
		arr[length-1-i] = temp
	}
	fmt.Printf("反转后arr: %v\n", arr)
}
